package com.chlp.core.framework.codeGenerator.util;

import com.chlp.core.framework.codeGenerator.entity.BasisInfo;
import com.chlp.core.framework.codeGenerator.entity.JsonResult;
import com.chlp.core.framework.codeGenerator.entity.PropertyInfo;

import java.util.List;

/**
 * Copyright: Copyright (c) 2019
 *
 * <p>说明：  获取文件路径调用创建文件</P>
 *
 * <p>
 * Modification History:
 * Date         	Author          Version          Description
 * ---------------------------------------------------------------*
 *
 * @author chlp1
 */
public class Generator {

	/**
	 * 路径信息
	 */
	private static final String ENTITY = "entity";
	private static final String DAO = "dao";
	private static final String DAO_IMPL = "daoImpl";
	private static final String SERVICE = "service";
	private static final String SERVICE_IMPL = "serviceImpl";
	private static final String CONTROLLER = "controller";
	private static final String SWAGGER_CONFIG = "swaggerConfig";
	private static final String PAGE_BEAN = "pageBean";

	/**
	 * ①创建实体类
	 */
	public static JsonResult createEntity(String url, BasisInfo bi) {
		String fileUrl = getGeneratorFileUrl(url, bi.getEntityUrl(), bi.getEntityName(), ENTITY);
		return FreemarkerUtil.createFile(bi, "entity.ftl", fileUrl);
	}

	/**
	 * ②创建DAO
	 */
	public static JsonResult createDao(String url, BasisInfo bi) {
		String fileUrl = getGeneratorFileUrl(url, bi.getDaoUrl(), bi.getEntityName(), DAO);
		return FreemarkerUtil.createFile(bi, "dao.ftl", fileUrl);
	}

	/**
	 * ③创建mapper配置文件
	 */
	public static JsonResult createDaoImpl(String url, BasisInfo bi) {
		String fileUrl = getGeneratorFileUrl(url, bi.getMapperUrl(), bi.getEntityName(), DAO_IMPL);
		List<PropertyInfo> list = bi.getCis();
		StringBuilder agile = new StringBuilder();
		for (PropertyInfo propertyInfo : list) {
			agile.append(propertyInfo.getColumn()).append(", ");
		}
		agile = new StringBuilder(agile.substring(0, agile.length() - 2));
		bi.setAgile(agile.toString());
		return FreemarkerUtil.createFile(bi, "mapper.ftl", fileUrl);
	}

	/**
	 * ④创建SERVICE
	 */
	public static JsonResult createService(String url, BasisInfo bi) {
		String fileUrl = getGeneratorFileUrl(url, bi.getServiceUrl(), bi.getEntityName(), SERVICE);
		return FreemarkerUtil.createFile(bi, "service.ftl", fileUrl);
	}

	/**
	 * ⑤创建SERVICE_IMPL
	 */
	public static JsonResult createServiceImpl(String url, BasisInfo bi) {
		String fileUrl = getGeneratorFileUrl(url, bi.getServiceImplUrl(), bi.getEntityName(), SERVICE_IMPL);
		return FreemarkerUtil.createFile(bi, "serviceImpl.ftl", fileUrl);
	}

	/**
	 * ⑥创建CONTROLLER
	 */
	public static JsonResult createController(String url, BasisInfo bi) {
		String fileUrl = getGeneratorFileUrl(url, bi.getControllerUrl(), bi.getEntityName(), CONTROLLER);
		bi.setIdType("string");
		return FreemarkerUtil.createFile(bi, "controller.ftl", fileUrl);
	}

	/**
	 * ⑥创建CONTROLLER
	 */
	public static JsonResult createSwaggerConfig(String url, BasisInfo bi) {
		String fileUrl = getGeneratorFileUrl(url, bi.getSwaggerConfigUrl(), "Swagger", SWAGGER_CONFIG);
		return FreemarkerUtil.createFile(bi, "SwaggerConfig.ftl", fileUrl);
	}

	/**
	 * pageBean
	 */
	public static JsonResult createPageBean(String url, BasisInfo bi) {
		String fileUrl = getGeneratorFileUrl(url, bi.getBeanUrl(), bi.getEntityName(), PAGE_BEAN);
		return FreemarkerUtil.createFile(bi, "pageBean.ftl", fileUrl);
	}

	/**
	 * commonMapper
	 */
	public static JsonResult createCommonMapper(String url, BasisInfo bi) {
		bi.setEntityName("Common");
		String fileUrl = getGeneratorFileUrl(url, bi.getMapperUrl(), bi.getEntityName(), DAO_IMPL);
		return FreemarkerUtil.createFile(bi, "commonMapper.ftl", fileUrl);
	}

	/**
	 * CommonDao
	 */
	public static JsonResult createCommonDao(String url, BasisInfo bi) {
		bi.setEntityName("Common");
		String fileUrl = getGeneratorFileUrl(url, bi.getDaoUrl(), bi.getEntityName(), DAO);
		return FreemarkerUtil.createFile(bi, "commonDao.ftl", fileUrl);
	}


	public static JsonResult createCrudUtils(String url, BasisInfo bi) {
		String mulTableQuery = url + bi.getCommonUrl() + "MulTableQuery.java";
		String commonCurdPageBean = url + bi.getCommonUrl() + "CommonCurdPageBean.java";
		String commonCurdResultBean = url + bi.getCommonUrl() + "CommonCurdResultBean.java";
		FreemarkerUtil.createFile(bi, "MulTableQuery.ftl", mulTableQuery);
		FreemarkerUtil.createFile(bi, "CommonCurdPageBean.ftl", commonCurdPageBean);
		FreemarkerUtil.createFile(bi, "CommonCurdResultBean.ftl", commonCurdResultBean);
		return null;
	}

	/**
	 * 生成文件路径和名字
	 */
	private static String getGeneratorFileUrl(String url, String packageUrl, String entityName, String type) {
		if ("entity".equals(type)) {
			return url + pageToUrl(packageUrl) + entityName + ".java";
		}
		if ("dao".equals(type)) {
			return url + pageToUrl(packageUrl) + entityName + "Dao.java";
		}
		if ("daoImpl".equals(type)) {
			return url + pageToUrl(packageUrl) + entityName + "Mapper.xml";
		}
		if ("service".equals(type)) {
			return url + pageToUrl(packageUrl) + entityName + "Service.java";
		}
		if ("serviceImpl".equals(type)) {
			return url + pageToUrl(packageUrl) + entityName + "ServiceImpl.java";
		}
		if ("controller".equals(type)) {
			return url + pageToUrl(packageUrl) + entityName + "Controller.java";
		}
		if ("swaggerConfig".equals(type)) {
			return url + pageToUrl(packageUrl) + entityName + "Config.java";
		}
		if ("pageBean".equals(type)) {
			return url + pageToUrl(packageUrl) + entityName + "PageBean.java";
		}
		return null;
	}

	private static String pageToUrl(String url) {
		return url.replace(".", "/") + "/";
	}
}
